//
//  AppTableViewController.m
//  应用管理02
//
//  Created by 伍银荣 on 2021/6/9.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "AppTableViewController.h"
#import "AppsModel.h"
#import "AppsTableViewCell.h"

@interface AppTableViewController ()<AppsTableViewCellDelegate>
@property (nonatomic, strong) NSArray *appsArr;
@end

@implementation AppTableViewController

-(NSArray *)appsArr {
    if (!_appsArr) {
        NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"data.plist" ofType:nil]];
        NSMutableArray *mArr = [NSMutableArray array];
        for (NSDictionary *dict in arr) {
            AppsModel *model = [AppsModel AppsWithDictionary:dict];
            [mArr addObject:model];
        }
        _appsArr = mArr;
    }
    return _appsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.rowHeight = 80;
    self.tableView.tableFooterView = [UIView new];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.appsArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"apps_cell";
    AppsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    cell.model = self.appsArr[indexPath.row];
    cell.delegate = self;
    return cell;
}

#pragma mark - AppsTableViewCellDelegate
-(void)createLabelWithAppsTableViewCell:(AppsTableViewCell *)appsCell {
    UILabel *textLabel = [[UILabel alloc]init];
    CGFloat textW = 150;
    CGFloat textH = 40;
    //    CGFloat textX = (Width - textW) / 2;
    //    CGFloat textY = (Height - textH) /2;
    textLabel.frame = CGRectMake(0, 0, textW, textH);
    textLabel.center = self.view.center;
    textLabel.backgroundColor = [UIColor blackColor];
    textLabel.text = @"正在下载";
    textLabel.textColor = [UIColor redColor];
    textLabel.font = [UIFont boldSystemFontOfSize:16];
    textLabel.textAlignment = NSTextAlignmentCenter;
    textLabel.alpha = 0.0;
    textLabel.layer.cornerRadius = 10;
    textLabel.layer.masksToBounds = YES;
    // 不能直接使用self.view添加label,因为滑到后面的cell,self.view没有改变,就看不到label了
    //[self.view addSubview:textLabel];
    [[[UIApplication sharedApplication] keyWindow] addSubview:textLabel];
    
    
    [UIView animateWithDuration:1.0 animations:^{
        textLabel.alpha = 0.6;
    } completion:^(BOOL finished) {
        if (finished) {
            //上一条动画完成后重新开启一个动画,并且延迟0.5秒后进行下一个动画
            [UIView animateWithDuration:1.0 delay:0.5 options:UIViewAnimationOptionCurveLinear animations:^{
               textLabel.alpha = 0.0;
            } completion:^(BOOL finished) {
                [textLabel removeFromSuperview];
            }];
        }
    }];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
